import axios from 'axios';
import { ElMessage } from "element-plus";
 import 'element-plus/theme-chalk/el-message.css'
 import {  useUserStore} from "@/stores/module/user";
let request= axios.create(
    {
        baseURL:import.meta.env.VITE_APP_BASE_API,
        timeout:10000
    }
) 
request.interceptors.request.use(function (config) {
    // Do something before request is sent
    const userStore=useUserStore()
    
    if(userStore.usertoken){
      config.headers.token=userStore.usertoken
    
    }
   
    return config;
  }, function (error) {
   
 console.log(error)
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
request.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
   
    return response.data;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    ElMessage({
      type:'error',
      message:'请求错误'
    })
    console.log(error)
    return Promise.reject(error);
  });


export default request